--
-- Author: yawei.zhu
-- Date: 2018-05-10 19:39:50
--
-- BranchTaskRender
--

local IconRender = require("app.widget.renders.IconRender")

local ClassRef = app.mvc.createRenderClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.TaskSideItemSubNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self:setContentSize(self._ui["Panel"]:getContentSize())
    self._iconRender = IconRender.create(self._ui.Base_icon)
    self._iconRender:setTipEnable(true)

    self:refresh()
end

function ClassRef:setDropItemNode(root, dropItem)
    local panelRoot = root:getChildByName("Panel")
    if dropItem then
        root:setVisible(true)
        local uiItem = IconRender.create(root)
        uiItem:setData(dropItem)
        uiItem:setTipEnable(true)
    else
        root:setVisible(false)
    end
end

function ClassRef:refresh()
    if not self._data then return end

    local task = self._data
    local taskId = task:getId()

    local rootPanel = self._ui["Panel"]
    local panelBG = rootPanel:getChildByName("Panel_bg")
    local btnItem = panelBG:getChildByName("bg01")
    local grade = panelBG:getChildByName("bg04")
    btnItem:setZoomScale(0)

    local qualityColor = Enums.QualityColor[tonumber(task:getConfig().quality) or 1]

    local iconNode = rootPanel:getChildByName("icon")
    grade:loadTexture(Res.commonUIPath .. "common_cell_rw_1.png")
    task:setTaskIcon(iconNode, grade, self._iconRender) -- TODO

    for i = 1, 2 do
        local name = "icon_reward"
        name = (i > 1) and name .. "_" .. ((i - 1) ) or name
        local rewardNode = rootPanel:getChildByName(name)
        local dropItems = TaskManager:getTaskDropItems(taskId, task:getConfig())
        self:setDropItemNode(rewardNode, dropItems[i])
    end

    local iconOverNode = rootPanel:getChildByName("icon_over")
    local btnGo = rootPanel:getChildByName("Button_go")
    local btnTrack = rootPanel:getChildByName("selected_btn")
    local btnGet = rootPanel:getChildByName("Button_lingqu")
    local nameNode = rootPanel:getChildByName("name")
    local desNode = rootPanel:getChildByName("Text")
    local numNode = rootPanel:getChildByName("num")
    numNode:setVisible(false)
    btnGo:setVisible(false)
    btnGet:setVisible(false)
    iconOverNode:setVisible(false)
    btnTrack:setVisible(false)

    local btnName = btnTrack:getChildByName("name_btn")
    btnName:setString(L("task_branch_desc2"))

    local isNeedTrack = executeFunc(self._onClick, "isNeedTrack", task:getId())
    btnTrack:setBright(not isNeedTrack)

    local taskConfig = task:getConfig()

    nameNode:setTextColor(qualityColor)
    nameNode:setString(L(taskConfig.name))
    desNode:setString(L(taskConfig.target))

    if task:isTaken() then
        btnTrack:setVisible(true)

        -- 设置进度
        local doneProgress = task:getDefaultDone()
        local totalProgress = task:getDefaultTotal()
        numNode:setVisible(true)
        numNode:setString(doneProgress .. "/" .. totalProgress)
    elseif task:isFinished() then
        btnGet:setVisible(true)
    elseif task:isRewarded() then
        iconOverNode:setVisible(true)
    end

    btnTrack:addClickEventListener(function(sender)
        local status = not sender:isBright()
        sender:setBright(status)
        self._onClick("TASK_TO_LIST", {add = not status, taskId = task:getId()})
    end)
    btnGet:addClickEventListener(function(sender)
        btnGet:setVisible(false) -- 防止连续点击
        self._onClick("GET_TASK_REWARD", {taskId = taskId, taskType = "branch"})
    end)

end

function ClassRef:setBtnTrack(flag)
    local btn = self._ui["Panel"]:getChildByName("selected_btn")
    btn:setBright(not flag)
end

-- 领奖返回
function ClassRef:getBranchTaskBack()
    local rootPanel = self._ui["Panel"]
    local iconOverNode = rootPanel:getChildByName("icon_over")
    local btnTrack = rootPanel:getChildByName("selected_btn")
    local btnGet = rootPanel:getChildByName("Button_lingqu")

    btnGet:setVisible(false)
    btnTrack:setVisible(false)
    iconOverNode:setVisible(true)

    local numNode = self._ui["Panel/num"]
    numNode:setVisible(false)
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
